

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INIT

% parameters
NCOMP = 1; % number of mixture components
USE_WEEKEND = 0; % including weekend tag in models
PRINT_ALL = 0; % print all cluster divergence scores

% path
oldPath = path;
addpath(genpath('../../data/ours/'));

% meta-feature labels
fid = fopen('mflabels.txt');
C = textscan(fid, '%d %s');
fclose(fid);
metafeatureIDs = C{1};
metafeatureLABELs = C{2};
clear C;
numMetafeatures = length(metafeatureIDs);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  TRAIN  %
%                                                                         %
%  A meta-feature classifier is built by computing a MoG for each sensor  %
%  and grouping these models by meta-feature. These clusters represent    %
%  our model for each meta-feature.                                       %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load firings_B_sec.txt;
if ~USE_WEEKEND,
  firings_B_sec = firings_B_sec(:,1:3);
end
load mfmap_B.txt;
sensorIDs_B = mfmap_B(:,1);
numSensors_B = length(sensorIDs_B);

allmog_B = cell(numSensors_B,1);
mf_clusters_B = cell(numMetafeatures,1);

fprintf('Computing mixture models for sensors of house B.\n');

for idx=1:numSensors_B,
  X = firings_B_sec(firings_B_sec(:,1)==sensorIDs_B(idx),2:end);
  allmog_B{idx} = gm_process_sensor(X, 'Blue', 0, NCOMP);
end

fprintf('Computing meta-feature clusters for house B.\n');

for idx=1:numMetafeatures,
  mog_ids = mfmap_B(:,2)==metafeatureIDs(idx);
  fprintf('Meta-feature %2d (%s): %d sensors.\n', ...
      metafeatureIDs(idx),metafeatureLABELs{idx},sum(mog_ids));
  if sum(mog_ids) == 0,
    % no sensors corresponding to this meta-feature
    mf_clusters_B{idx} = cell(0);
  else
    mf_clusters_B{idx} = allmog_B(mog_ids);
  end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  TEST  %
%                                                                         %
%  Find for each 'anonymous' cluster (meta-feature of house C) the        %
%  best 'candidate' match (meta-feature of house B).                      %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load firings_C_sec.txt;
if ~USE_WEEKEND,
  firings_C_sec = firings_C_sec(:,1:3);
end
load mfmap_C.txt;
sensorIDs_C = mfmap_C(:,1);
numSensors_C = length(sensorIDs_C);

fprintf('\nComputing mixture models for sensors of house C.\n');

allmog_C = cell(numSensors_C,1);
for idx=1:numSensors_C,
  X = firings_C_sec(firings_C_sec(:,1)==sensorIDs_C(idx),2:end);
  allmog_C{idx} = gm_process_sensor(X, 'Blue', 0, NCOMP);
end

fprintf('Computing meta-feature clusters for house C.\n');

mf_clusters_C = cell(numMetafeatures,1);
for idx=1:numMetafeatures,
  mog_ids = mfmap_C(:,2)==metafeatureIDs(idx);
  fprintf('Meta-feature %2d (%s): %d sensors.\n', ...
      metafeatureIDs(idx),metafeatureLABELs{idx},sum(mog_ids));
  if sum(mog_ids) == 0,
    % no sensors corresponding to this meta-feature
    mf_clusters_C{idx} = cell(0);
  else
    mf_clusters_C{idx} = allmog_C(mog_ids);
  end
end

fprintf('\nFinding best matches for houseC->houseB (normal).\n');

% for each 'anonymous' cluster in houseC
for idxAnonymous=1:length(mf_clusters_C),
    
  anonymous = mf_clusters_C{idxAnonymous};
  
  if isempty(anonymous),
    continue;
  end
  
  fprintf('Compute divergence scores for %s/C.\n', ...
    metafeatureLABELs{idxAnonymous});
  
  minDiv = inf;
  ordCandidate = inf;

  % find best matching 'candidate' cluster from houseA
  for idxCandidate=1:numMetafeatures,
    candidate = mf_clusters_B{idxCandidate};
    curDiv = compute_cluster_divergence(anonymous, candidate);
    
    if PRINT_ALL && curDiv ~= inf,
      fprintf('  -> %s/B (%f)\n', ...
        metafeatureLABELs{idxCandidate}, ...
        curDiv);
    end
    
    if curDiv < minDiv,
      minDiv = curDiv;
      ordCandidate = idxCandidate;
    end
  end
  
  if isinf(ordCandidate),
    continue;
  else    
    fprintf('Best match for %s/C: %s/B (%f)\n', ...
      metafeatureLABELs{idxAnonymous}, ...
      metafeatureLABELs{ordCandidate}, ...
      minDiv);
  end
  
end

% restore old MATLAB path
path(oldPath);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  RESULTS  %
%                                                                         %
%
%   NCOMP = 1
%
%   Computing mixture models for sensors of house B.
%   Computing meta-feature clusters for house B.
%   Meta-feature  1 (KitchHeat): 3 sensors.
%   Meta-feature  2 (Toilet): 1 sensors.
%   Meta-feature  3 (BathroomDoor): 1 sensors.
%   Meta-feature  4 (Kitchen): 3 sensors.
%   Meta-feature  5 (KitchStor): 2 sensors.
%   Meta-feature  6 (Outside): 1 sensors.
%   Meta-feature  7 (SleepDoor): 1 sensors.
%   Meta-feature  8 (Sleep): 4 sensors.
%   Meta-feature  9 (Bathroom): 2 sensors.
%   Meta-feature 10 (Other): 0 sensors.
% 
%   Computing mixture models for sensors of house C.
%   Computing meta-feature clusters for house C.
%   Meta-feature  1 (KitchHeat): 1 sensors.
%   Meta-feature  2 (Toilet): 3 sensors.
%   Meta-feature  3 (BathroomDoor): 1 sensors.
%   Meta-feature  4 (Kitchen): 6 sensors.
%   Meta-feature  5 (KitchStor): 2 sensors.
%   Meta-feature  6 (Outside): 1 sensors.
%   Meta-feature  7 (SleepDoor): 1 sensors.
%   Meta-feature  8 (Sleep): 3 sensors.
%   Meta-feature  9 (Bathroom): 2 sensors.
%   Meta-feature 10 (Other): 1 sensors.
% 
%   Finding best matches for houseC->houseB (normal).
%   Compute divergence scores for KitchHeat/C.
%     -> KitchHeat/B (0.902247)
%     -> SleepDoor/B (1.132305)
%     -> BathroomDoor/B (1.375366)
%     -> KitchStor/B (1.654862)
%     -> Toilet/B (2.598332)
%     -> Sleep/B (4.054868)
%     -> Bathroom/B (5.720164)
%     -> Outside/B (7.958822)
%     -> Kitchen/B (192.843484)
%   Best match for KitchHeat/C: KitchHeat/B (0.902247)
%
%   Compute divergence scores for Toilet/C.
%     -> BathroomDoor/B (0.562101)
%     -> Bathroom/B (0.989957)
%     -> Toilet/B (1.449720)
%     -> SleepDoor/B (2.817459)
%     -> Sleep/B (3.108148)
%     -> Kitchen/B (5.275320)
%     -> Outside/B (7.281098)
%     -> KitchStor/B (15.077809)
%     -> KitchHeat/B (15.630788)
%   Best match for Toilet/C: BathroomDoor/B (0.562101)
%
%   Compute divergence scores for BathroomDoor/C.
%     -> KitchHeat/B (1.448432)
%     -> Kitchen/B (11.483373)
%     -> Sleep/B (13.738776)
%     -> KitchStor/B (16.443881)
%     -> Bathroom/B (18.440875)
%     -> SleepDoor/B (207.795090)
%     -> BathroomDoor/B (210.018894)
%     -> Toilet/B (210.735047)
%     -> Outside/B (216.759716)
%   Best match for BathroomDoor/C: KitchHeat/B (1.448432)
%
%   Compute divergence scores for Kitchen/C.
%     -> BathroomDoor/B (0.295733)
%     -> Bathroom/B (0.680212)
%     -> KitchStor/B (0.826681)
%     -> Toilet/B (1.142954)
%     -> SleepDoor/B (1.262884)
%     -> Outside/B (1.631504)
%     -> Sleep/B (1.936638)
%     -> KitchHeat/B (6.091807)
%   Best match for Kitchen/C: BathroomDoor/B (0.295733)
%
%   Compute divergence scores for KitchStor/C.
%     -> BathroomDoor/B (0.253238)
%     -> Bathroom/B (0.562109)
%     -> Toilet/B (1.208270)
%     -> SleepDoor/B (1.300266)
%     -> KitchHeat/B (1.846324)
%     -> Sleep/B (4.651888)
%     -> KitchStor/B (6.367817)
%     -> Outside/B (6.741429)
%     -> Kitchen/B (174.276899)
%   Best match for KitchStor/C: BathroomDoor/B (0.253238)
%
%   Compute divergence scores for Outside/C.
%     -> BathroomDoor/B (0.343082)
%     -> Toilet/B (1.361371)
%     -> Bathroom/B (1.380528)
%     -> KitchStor/B (1.413452)
%     -> KitchHeat/B (2.847975)
%     -> Sleep/B (4.393555)
%     -> Outside/B (6.830286)
%     -> SleepDoor/B (21.598236)
%     -> Kitchen/B (158.305872)
%   Best match for Outside/C: BathroomDoor/B (0.343082)
%
%   Compute divergence scores for SleepDoor/C.
%     -> BathroomDoor/B (0.691177)
%     -> Sleep/B (0.880509)
%     -> Toilet/B (1.266244)
%     -> Bathroom/B (2.889053)
%     -> KitchHeat/B (3.336938)
%     -> KitchStor/B (3.974818)
%     -> Outside/B (7.692989)
%     -> SleepDoor/B (14.523207)
%     -> Kitchen/B (174.766483)
%   Best match for SleepDoor/C: BathroomDoor/B (0.691177)
%
%   Compute divergence scores for Sleep/C.
%     -> BathroomDoor/B (0.752961)
%     -> Sleep/B (0.992944)
%     -> Bathroom/B (1.382250)
%     -> Toilet/B (1.470752)
%     -> SleepDoor/B (6.141726)
%     -> Kitchen/B (6.493530)
%     -> Outside/B (7.623680)
%     -> KitchHeat/B (26.810574)
%     -> KitchStor/B (28.352834)
%   Best match for Sleep/C: BathroomDoor/B (0.752961)
%
%   Compute divergence scores for Bathroom/C.
%     -> Toilet/B (0.078664)
%     -> BathroomDoor/B (0.845563)
%     -> Sleep/B (1.332077)
%     -> Bathroom/B (3.028171)
%     -> KitchHeat/B (4.207239)
%     -> Outside/B (5.414355)
%     -> Kitchen/B (58.427753)
%     -> KitchStor/B (81.276089)
%     -> SleepDoor/B (92.775544)
%   Best match for Bathroom/C: Toilet/B (0.078664)
%
%   Compute divergence scores for Other/C.
%     -> Kitchen/B (0.136993)
%     -> Sleep/B (0.565964)
%     -> KitchStor/B (2.789391)
%     -> Outside/B (3.038505)
%     -> Bathroom/B (4.573418)
%     -> KitchHeat/B (6.526048)
%     -> Toilet/B (59.487416)
%     -> BathroomDoor/B (130.644450)
%     -> SleepDoor/B (198.837487)
%   Best match for Other/C: Kitchen/B (0.136993)
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
